package utils

import (
	"sync"

	"go.uber.org/zap"
)

//var logs map[string]*zap.Logger

const (
	DefaultLoggerName = "default"
	ApiLoggerName     = "api"
	DefaultOut        = "stderr"
	DefaultLevel      = -1
)

var logg *loggers

type loggers struct {
	logs map[string]*zap.Logger
	sync.RWMutex
}

func (l *loggers) addLogger(name string, logger *zap.Logger) {
	l.Lock()
	defer l.Unlock()
	l.logs[name] = logger
	return
}

func (l *loggers) getLogger(name string) *zap.Logger {
	l.RLock()
	defer l.RUnlock()
	return l.logs[name]
}

func init() {
	logg = &loggers{
		logs: make(map[string]*zap.Logger),
	}
}

func Log(name ...string) *zap.Logger {
	if len(name) == 0 {
		name = []string{DefaultLoggerName}
	}

	return logg.getLogger(name[0])
}

func SetLog(name string, logger *zap.Logger) {
	logg.addLogger(name, logger)
}
